package com.redstar.interesting.common.starter.multiDataSource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.redstar.interesting.common.starter.mutiDataSource2.BaseDataSourceConfig;
import com.redstar.interesting.common.starter.mutiDataSource2.MultiDataSourceProperties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lihongxing
 */
@Configuration
public class DynamicDataSourceConfig {

    @Autowired
    private MybatisProperties properties;

    @Autowired
    private MultiDataSourceProperties multiDataSourceProperties;


    @Primary
    @Bean("masterDataSource")
    @ConfigurationProperties("spring.datasource.druid.master")
    public DataSource masterDataSource(@Autowired DruidProperties druidProperties) {
        return druidProperties.dataSource(DruidDataSourceBuilder.create().build());
    }


    @Bean("slaveDataSource")
    @ConfigurationProperties("spring.datasource.druid.slave")
    public DataSource slaveDataSource(@Autowired DruidProperties druidProperties) {
        return druidProperties.dataSource(DruidDataSourceBuilder.create().build());
    }


    @Bean("dynamicDataSource")
    public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                               @Qualifier("slaveDataSource") DataSource slaveDataSource) {
        Map<Object, Object> targetDataSource = new HashMap<>(2);
        targetDataSource.put(EnumDataSource.MASTER.name(), masterDataSource);
        targetDataSource.put(EnumDataSource.SLAVE.name(), slaveDataSource);
        List<BaseDataSourceConfig> baseDataSourceConfig = multiDataSourceProperties.getCustomerDataSourceList();
        return new DynamicDataSource(masterDataSource, targetDataSource);
    }

    @Primary
    @Bean
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(masterDataSource);
        sqlSessionFactory.setConfiguration(properties.getConfiguration());
        sqlSessionFactory.setMapperLocations(properties.resolveMapperLocations());
        return sqlSessionFactory.getObject();
    }


    @Bean
    public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(slaveDataSource);
        sqlSessionFactory.setConfiguration(properties.getConfiguration());
        sqlSessionFactory.setMapperLocations(properties.resolveMapperLocations());
        return sqlSessionFactory.getObject();
    }

}
